import { ref } from 'vue';
import dayjs from 'dayjs';
import { useAuthStore } from '@/store/modules/auth';
let topFormModel;
let bottomFormModel;
const lotType = ref('searchInput');
const lotids = ref([]);
const moduleNoSearchData = ref(null);
const authStore = useAuthStore();

export default {
  url: '/qms/single/afterOven/list',
  addUrl: '/qms/single/afterOven/add',
  editUrl: '/qms/single/afterOven/update',
  delUrl: '/qms/single/afterOven/deleteBatch',
  editMethod: 'post',
  searchFormList: [
    {
      type: 'input',
      key: 'moduleNo',
      label: '生产型号',
      placeholder: '请输入生产型号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4
    },
    {
      type: 'input',
      key: 'lotId',
      label: 'LOT号',
      placeholder: '请输入LOT号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4
    },
    {
      type: 'input',
      key: 'serialNum',
      label: '单据号',
      placeholder: '请输入单据号',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4
    },
    {
      type: 'select',
      key: 'state',
      label: '审核状态',
      placeholder: '请选择审核状态',
      span: 24,
      md: 12,
      lg: 8,
      xl: 8,
      xxl: 4,
      selectOptions: [
        {
          value: '0',
          label: '待领班审核'
        },
        {
          value: '1',
          label: '待修改'
        },
        {
          value: '2',
          label: '待主管审核'
        },
        {
          value: '3',
          label: '审核完成'
        }
      ]
    },
    {
      type: 'dateRangePicker',
      key: 'time',
      label: '日期',
      span: 24,
      md: 12,
      lg: 10,
      xl: 8,
      xxl: 6
    }
  ],
  column: [
    {
      key: 'dataIndex',
      dataIndex: 'dataIndex',
      title: '序号',
      width: 70,
      fixed: 'left',
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'moduleNo',
      dataIndex: 'moduleNo',
      title: '生产型号',
      width: 150,
      align: 'center',
      fixed: 'left',
      checked: true,
      ellipsis: true
    },
    {
      key: 'lotId',
      dataIndex: 'lotId',
      title: 'LOT号',
      width: 150,
      align: 'center',
      fixed: 'left',
      checked: true,
      ellipsis: true
    },
    {
      key: 'serialNum',
      dataIndex: 'serialNum',
      title: '单据号',
      width: 130,
      align: 'center',
      fixed: 'left',
      checked: true,
      ellipsis: true
    },
    {
      key: 'state',
      dataIndex: 'state',
      title: '状态',
      width: 110,
      align: 'center',
      checked: true,
      ellipsis: true,
      customRender: ({ record }) => {
        return ['待领班审核', '待修改', '待主管审核', '审核完成'][record.state];
      }
    },
    {
      key: 'modifyTime',
      dataIndex: 'modifyTime',
      title: '录入时间',
      width: 170,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'palateEntryTime',
      dataIndex: 'palateEntryTime',
      title: '进板时间',
      width: 170,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'palateOutTime',
      dataIndex: 'palateOutTime',
      title: '出板时间',
      width: 170,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'quantity',
      dataIndex: 'quantity',
      title: '数量PNL',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'firstTemperature',
      dataIndex: 'firstTemperature',
      title: '第一段温度时间',
      width: 140,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'secondTemperature',
      dataIndex: 'secondTemperature',
      title: '第二段温度时间',
      width: 140,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'thirdTemperature',
      dataIndex: 'thirdTemperature',
      title: '第三段温度时间',
      width: 140,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'notDry',
      dataIndex: 'notDry',
      title: '是否烤干',
      width: 110,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'operator',
      dataIndex: 'operator',
      title: '操作员',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },

    {
      key: 'foreman',
      dataIndex: 'foreman',
      title: '领班审核',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'foremanTime',
      dataIndex: 'foremanTime',
      title: '领班审核时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'examiner',
      dataIndex: 'examiner',
      title: '主管审核',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'examinerTime',
      dataIndex: 'examinerTime',
      title: '主管审核时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'remark',
      dataIndex: 'remark',
      title: '备注',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'createTime',
      dataIndex: 'createTime',
      title: '创建时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'updateTime',
      dataIndex: 'updateTime',
      title: '修改时间',
      width: 180,
      align: 'center',
      checked: true,
      ellipsis: true
    },
    {
      key: 'updateBy',
      dataIndex: 'updateBy',
      title: '修改人',
      width: 100,
      align: 'center',
      checked: true,
      ellipsis: true
    }
  ],
  searchParam: ref({
    prodNo: null,
    lotId: null,
    state: null,
    time: null
  }),
  scroll: { x: 2500, y: 'auto' },
  topFormList: [
    // 用于获取model
    {
      type: 'getModel',
      hidden: true,
      handle(topModel, personModle) {
        topFormModel = topModel;
        bottomFormModel = personModle;
      }
    },
    {
      type: 'searchInput',
      key: 'moduleNo',
      label: '生产型号',
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      span: 8,
      rule: {
        required: true,
        message: '请输入生产型号',
        trigger: 'change'
      },
      transfromSearch(model, searchData) {
        topFormModel = model;
        moduleNoSearchData.value = searchData;
        Object.keys(model.value).forEach(item => {
          model.value[item] = searchData[item] || model.value[item];
        });
        lotids.value =
          searchData?.lotNo?.map(item => {
            return {
              label: item.lotNo,
              value: item.lotNo
            };
          }) || [];
        if (lotids.value.length) {
          lotType.value = 'select';
          model.value.lotId = null;
        } else {
          lotType.value = 'searchInput';
        }
      }
    },
    {
      type: lotType,
      key: 'lotId',
      label: 'Lot号',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      disabled: false,
      rule: {
        required: true,
        message: '请输入Lot号',
        trigger: 'change'
      },
      searchKey: 'lotNumber',
      transfromSearch(model, searchData) {
        topFormModel = model;
        moduleNoSearchData.value = searchData;
        Object.keys(model.value).forEach(item => {
          model.value[item] = searchData[item] || model.value[item];
        });
      },
      selectOptions: lotids
    },
    // moduleNo
    {
      type: 'datePicker',
      key: 'palateEntryTime',
      label: '进板时间',
      span: 8,
      rule: {
        required: true,
        message: '请选择进板时间',
        trigger: 'change'
      },
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      showTime: true,
      // 选择进板时间之后，自动计算出板时间，出板时间是进板时间推迟2个半小时之后的日期
      change(e) {
        console.log(e);
        const delayTime = 150 * 60 * 1000; // 2个半小时
        topFormModel.value.palateOutTime = dayjs(new Date(e).getTime() + delayTime).format('YYYY-MM-DD HH:mm:ss');
      }
    },
    {
      type: 'datePicker',
      key: 'palateOutTime',
      label: '出板时间',
      span: 8,
      rule: {
        required: true,
        message: '请选择出板时间',
        trigger: 'change'
      },
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      showTime: true
    },
    {
      type: 'datePicker',
      key: 'modifyTime',
      label: '录入时间',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      showTime: true
    },
    {
      type: 'textarea',
      key: 'remark',
      label: '备注',
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      textareaRows: 1,
      span: 24
    },
    {
      type: 'tabs',
      span: 24,
      name: '生产条件'
    },
    {
      type: 'input',
      key: 'quantity',
      label: '数量',
      rule: {
        required: true,
        message: '请输入生产数量',
        trigger: 'change'
      },
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      }
    },
    {
      type: 'select',
      key: 'notDry',
      label: '是否烤干',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: [
        {
          value: '已烤干',
          label: '已烤干'
        },
        {
          value: '未烤干',
          label: '未烤干'
        }
      ],
      defaultValue: '已烤干'
    },
    {
      type: '',
      span: 8
    },
    {
      type: 'tabs',
      name: '温度℃/时间min',
      span: 24
    },
    {
      type: 'select',
      key: 'firstTemperature',
      placeholder: '请选择第一段温度',
      label: '第一段',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      selectOptions: [
        {
          value: '90/30',
          label: '90/30'
        },
        {
          value: '110/60',
          label: '110/60'
        },
        {
          value: '155/60',
          label: '155/60'
        }
      ],
      change(e) {
        console.log(e);
        if (e === '90/30') {
          topFormModel.value.secondTemperature = '110/60';
          topFormModel.value.thirdTemperature = '155/60';
        } else if (e === '110/60') {
          topFormModel.value.secondTemperature = '';
          topFormModel.value.thirdTemperature = '';
        } else if (e === '155/60') {
          topFormModel.value.secondTemperature = '155/30';
          topFormModel.value.thirdTemperature = '';
        }
      }
    },

    {
      type: 'input',
      key: 'secondTemperature',
      label: '第二段',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      change(e) {
        console.log(e);
      },
      showTime: true
    },
    {
      type: 'input',
      key: 'thirdTemperature',
      label: '第三段',
      span: 8,
      tansfromDisabled(operateType) {
        return operateType === 'look';
      },
      showTime: true
    }
  ],
  bottomList: [
    {
      type: 'input',
      key: 'operator',
      label: '操作员',
      defaultValue: authStore.userInfo.user.nickName || authStore.userInfo.user.userName,
      disabled: true,
      span: 8
    },
    {
      type: 'input',
      key: 'foreman',
      label: '领班审核',
      disabled: true,
      span: 8
    },
    {
      type: 'input',
      key: 'examiner',
      label: '主管审核',
      disabled: true,
      span: 8
    }
  ],
  modalHeight: '90vh',
  modleSearchUrlObj: {
    lotId: '/rcerp/agv/erp/findByLotNo',
    moduleNo: '/rcerp/agv/erp/findByModuleNo'
  },
  modalGetTableUrlSuffix: {
    add: 'selectBadId',
    byId: 'listBad'
  },
  modalClose() {
    lotType.value = 'searchInput';
    moduleNoSearchData.value = null;
    lotids.value = [];
  }
};
